//子类中没有使用动态内存，只有基类中使用了动态内存
#include <iostream>
#include <string.h>
using namespace std;

class Base
{
    char *p;
    int size;
    
public:
    Base(int s = 1){size = s; p = new char[s];}

    //复写拷贝构造函数、赋值操作函数，以此提供深拷贝的逻辑
    Base(const Base &r);
    Base &operator=(const Base &r);

    ~Base(){delete [] p;}
};

Base::Base(const Base &r)
{
    this->size = r.size;
    this->p = new char[this->size];
    memcpy(p,r.p,r.size);
}

Base &Base::operator=(const Base &r)
{
    delete [] this->p;

    this->size = r.size;
    this->p = new char[this->size];
    memcpy(p,r.p,r.size);

    return *this;
}

class Derived : public Base
{
public:
    Derived(int s = 0):Base(s){}
};

int main(int argc, char const *argv[])
{
    Derived d1;

    Derived d2(100);

    Derived d3(d2);

    d1 = d2;

    //内存不会产生泄露

    return 0;
}

